package com.whc.gulimall.product.app;

import com.whc.common.utils.PageUtils;
import com.whc.common.utils.R;
import com.whc.gulimall.product.entity.AttrEntity;
import com.whc.gulimall.product.entity.AttrGroupEntity;
import com.whc.gulimall.product.service.AttrGroupService;
import com.whc.gulimall.product.service.AttrService;
import com.whc.gulimall.product.vo.AttrAttrGroupRelationVo;
import com.whc.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * 属性分组
 *
 * @author wanghaochen
 * @email angaoen@outlook.com
 * @date 2022-03-05 00:18:53
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {

    /**
     * 属性分组 Service
     */
    @Resource
    private AttrGroupService attrGroupService;

    /**
     * 商品属性 Service
     */
    @Resource
    private AttrService attrService;

    /**
     * 获取列表
     *
     * @param params 参数
     * @param catId  分类id
     * @return 数据
     */
    @GetMapping("/list/{catId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catId") Long catId) {
        PageUtils page = attrGroupService.queryPage(params, catId);
        return R.ok().put("page", page);
    }


    /**
     * 通过id获取到属性
     *
     * @param attrGroupId 分组id
     * @return 分组属性
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getInfoById(attrGroupId);
        return R.ok().put("data", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     *
     * @param deleteVos 封装好的数据集合
     * @return 同意返回结果
     */
    @DeleteMapping("/attr/relation/delete")
    public R delete(@RequestBody List<AttrAttrGroupRelationVo> deleteVos) {
        attrGroupService.removeAttrGroupByAttrIdsAndAttrGroupIds(deleteVos);
        return R.ok();
    }


    /**
     * 获取属性分组的关联的所有属性
     *
     * @param attrgroupId 属性分组id
     * @return 统一返回结果
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R getRelationAttr(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> attrEntityList = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data", attrEntityList);
    }

    /**
     * 获取属性分组没有关联的其他属性
     *
     * @param params      参数
     * @param attrgroupId 分组id
     * @return 统一返回结果
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R getNoRelationAttr(@RequestParam Map<String, Object> params,
                               @PathVariable("attrgroupId") Long attrgroupId) {
        PageUtils page = attrGroupService.getNoRelationAttr(params, attrgroupId);
        return R.ok().put("page", page);
    }

    /**
     * 添加属性与分组关联关系
     *
     * @param relationVos 关系列表
     * @return 统一返回对象
     */
    @PostMapping("/attr/relation")
    public R saveAttrRelation(@RequestBody List<AttrAttrGroupRelationVo> relationVos) {
        attrGroupService.saveAttrRelation(relationVos);
        return R.ok();
    }

    /**
     * 获取分类下所有分组&关联属性
     *
     * @param catelogId 三级分类id
     * @return 统一返回对象
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttr(@PathVariable("catelogId") Long catelogId) {
        List<AttrGroupWithAttrsVo> vos = attrGroupService.getAttrGroupWithAttrByCatelogId(catelogId);
        return R.ok().put("data", vos);
    }

    @DeleteMapping("/delete")
    public R deleteAttrGroupByIds(@RequestBody List<Long> attrGroupIds) {
        attrGroupService.deleteAttrGroupByIds(attrGroupIds);
        return R.ok();
    }

}
